package com.itheima.mq.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.HashMap;

/**
 * 启动类
 */
@SpringBootApplication
public class RabbitmqApplication {

    // 发送消息的模板类
    private static RabbitTemplate rabbitTemplate ;

    public static void main(String[] args) {

        // 启动程序
        ConfigurableApplicationContext applicationContext = SpringApplication.run(RabbitmqApplication.class, args);
        rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);

        // 查看AMQP协议生产者消息流转过程
        // producerMessageTransport() ;

        // 测试FanoutExchange交换机的消息发送
//         fanoutExchangeMessageTransport() ;

        // 测试Direct类型的交换机
         directExchangeMessageTransport();

        // 测试Topic类型的交换机的消息发送
//         topicExchangeMessageTransport() ;

        // 测试headers交换机的消息发送
//         headersExchangeMessageTransport();
        //测试延迟消息的发送
//        sendPreMessageTTL();

    }

    //设置消息发送时候的ttl
    private static void sendPreMessageTTL(){
        rabbitTemplate.convertAndSend("direct.exchange","create","测试超时消息",message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setExpiration("15000");
            return message;
        });
    }


    // 测试headers交换机的消息发送
    private static void headersExchangeMessageTransport() {
        String message = "headers exchanges message" ;
        MessageProperties messageProperties = new MessageProperties() ;
         messageProperties.setHeader("key1" , "value1");
        messageProperties.setHeader("key2" , "value2");
        Message obj = new Message(message.getBytes() , messageProperties) ;
        rabbitTemplate.convertAndSend("headers.exchange" , "" , obj);
    }

    // 测试Topic类型的交换机的消息发送
    private static void topicExchangeMessageTransport() {

        // routingKey: topic.email.queue  ---> topic.email
        // routingKey: topic.sms.queue  ---> topic.sms
        // routingKey: topic.email.sms  ---> topic.email && topic.sms
//        rabbitTemplate.convertAndSend("topic.exchange" , "topic.sms" , "topic exchange 测试数据");
//        rabbitTemplate.convertAndSend("topic.exchange" , "topic.ws.order.ws" , "topic exchange 测试数据");
        //TODO 关于#和*的路由问题
        rabbitTemplate.convertAndSend("topic.exchange" , "topic.order" , "topic exchange 测试数据");
    }

    // 测试Direct类型的交换机
    private static void directExchangeMessageTransport() {
//        rabbitTemplate.convertAndSend("direct.exchange" , "delete" , "direct exchange 测试数据");
        rabbitTemplate.convertAndSend("direct.exchange" , "create" , "direct exchange 测试数据");
        //TODO 测试超时队列数据，以及死信消息队列
        rabbitTemplate.convertAndSend("direct.exchange" , "ttl" , "direct exchange 测试数据");

    }

    // 测试FanoutExchange交换机的消息发送
    private static void fanoutExchangeMessageTransport() {
        rabbitTemplate.convertAndSend("fanout.exchange" , "itheima" , "fanout exchange 测试数据");
    }

    // 查看AMQP协议生产者消息流转过程测试代码
    private static void producerMessageTransport() {
        rabbitTemplate.convertAndSend("fanout.exchange" , "" , "rabbitmq生产者消息流程过程测试");
    }

}
